import { createContext, useContext } from 'react'
import { useWorkTimeData } from '../hooks/useWorkTimeData'
import { useUIState } from '../hooks/useUIState'

// 创建Context
const WorkTimeContext = createContext(null)

// Provider组件
export const WorkTimeProvider = ({ children }) => {
  // 工时数据状态和方法
  const workTimeDataMethods = useWorkTimeData()

  // UI状态和方法
  const uiStateMethods = useUIState()

  // 合并所有状态和方法
  const contextValue = {
    ...workTimeDataMethods,
    ...uiStateMethods,
  }

  return (
    <WorkTimeContext.Provider value={contextValue}>
      {children}
    </WorkTimeContext.Provider>
  )
}

// 自定义hook用于使用Context
// eslint-disable-next-line react-refresh/only-export-components
export const useWorkTime = () => {
  const context = useContext(WorkTimeContext)
  if (!context) {
    throw new Error('useWorkTime必须在WorkTimeProvider内部使用')
  }
  return context
}

export default WorkTimeContext
